package com.ttfeed.server.common.utils;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Calendar;
import java.util.Date;

@Slf4j
public class DateUtils {
    public static String getNumberOfWeek(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        switch (day){
            case 1: return "周日";
            case 2: return "周一";
            case 3: return "周二";
            case 4: return "周三";
            case 5: return "周四";
            case 6: return "周五";
            case 7: return "周六";
        }
        return "今天";
    }

    public static int getDiffDay(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance(); //创建Calendar对象并设置时区
        cal1.setTime(DateUtil.beginOfDay(date1));
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(DateUtil.beginOfDay(date2));
        long diffInMillies = Math.abs(cal2.getTime().getTime() - cal1.getTime().getTime()); //获取两个日期之间的毫秒数差值
        int daysDiff = (int)(diffInMillies / (1000 * 60 * 60 * 24)); //将毫秒数差值转换成天数
        return daysDiff;
    }

    public static void main(String[] args) {
        System.out.println(getNumberOfWeek(DateUtil.parseDate("2023-12-05 11:00:00")));
        getDiffDay(DateUtil.parseDate("2023-12-05 00:00:00"), DateUtil.parseDate("2023-12-01 23:00:00"));

        System.out.println(DateUtil.format(new Date(), "yyyy/MM/dd"));
        System.out.println(getHourAndMinute(new Date()));
    }

    public static String getHourAndMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        return hour + ":" + minute;
    }
}
